/**
 * Copyright (c) 2008 Andrew Rapp. All rights reserved.
 *  
 * This file is part of XBee-API.
 *  
 * XBee-API is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *  
 * XBee-API is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with XBee-API.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.rapplogic.xbee.api;

import java.util.HashMap;
import java.util.Map;

import com.rapplogic.xbee.util.ByteUtils;

public class AtCommandResponse extends XBeeFrameIdResponse {

    public /*enum*/ static class Status {

        public static final Status OK = new Status(0);
        public static final Status ERROR = new Status(1);
        public static final Status INVALID_COMMAND = new Status(2);
        public static final Status INVALID_PARAMETER = new Status(3);
        public static final Status NO_RESPONSE = new Status(4);  // series 1 remote AT only according to spec
        private static final Map lookup = new HashMap();


        static {
            lookup.put(OK.getValue(), OK);
            lookup.put(ERROR.getValue(), ERROR);
            lookup.put(INVALID_COMMAND.getValue(), INVALID_COMMAND);
            lookup.put(INVALID_PARAMETER.getValue(), INVALID_PARAMETER);
            lookup.put(NO_RESPONSE.getValue(), NO_RESPONSE);
        }

        public static Status get(int value) {
            return (Status) lookup.get(new Integer(value));
        }
        private final int value;

        Status(int value) {
            this.value = value;
        }

        public Integer getValue() {
            return new Integer(value);
        }

        public String toString() {
            switch (value) {
                case 0:
                    return "OK";
                case 1:
                    return "ERROR";
                case 2:
                    return "INVALID_COMMAND";
                case 3:
                    return "INVALID_PARAMETER";
                case 4:
                    return "NO_RESPONSE";
                default:
                    return "Undefined";

            }
        }
    }
    private int char1;
    private int char2;
    private Status status;
    // response value msb to lsb
    private int[] value;

    public AtCommandResponse() {
    }

    public int getChar1() {
        return char1;
    }

    public void setChar1(int char1) {
        this.char1 = char1;
    }

    public int getChar2() {
        return char2;
    }

    public void setChar2(int char2) {
        this.char2 = char2;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public boolean isOk() {
        return status == Status.OK;
    }

    public int[] getValue() {
        return value;
    }

    public void setValue(int[] data) {
        this.value = data;
    }

    public String getCommand() {
        return String.valueOf((char) this.char1) + String.valueOf((char) this.char2);
    }

    public String toString() {
        return super.toString() + ",command=" + this.getCommand() +
                ",status=" + this.getStatus() + ",value=" +
                (this.value == null ? "null" : ByteUtils.toBase16(this.getValue()));
    }
}